<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
/**
 * Description of GUsuario
 *
 * @author Luis Alberto
 */
include './libs/adodb/adodb.inc.php';
include './configuracion.php';
include 'Usuario.php';
class GUsuario
{
    private $db;
    private $usuario=null;
    public function __construct()
    {
        $this->usuario = new Usuario();
    }
    public function Guardar($usrn,$pass,$nomb,$apell,$dir,$ema,$tel,$ci,$per_co)
    {        
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        $pass = md5($pass); /*Encriptamos el Password en MD5*/        
        $cns = "select usr_cod from usuario where usr_username = '$usrn' or usr_email = '$ema'";
        $rs = $db->Execute($cns);
        if($rs->RecordCount()==0)
        {
                $cns = "insert into usuario (usr_username, usr_password, usr_nombre, usr_apellidos, usr_direccion, usr_email, usr_telefono , usr_ci, per_cod)";
                $cns .= " values ('$usrn','$pass','$nomb','$apell','$dir','$ema','$tel','$ci','$per_co')";
                $rs = $db->Execute($cns);
                return $rs;
        }else
        {
                return false;
        }
    }
    public function Modificar($cod,$usrn,$pass,$nomb,$apell,$dir,$ema,$tel,$ci,$per_co)
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
	$cns = "update usuario";
        $cns .= " set usr_name = $usrn, usr_password=$pass, usr_nombre=$nomb, usr_apellido=$apell, usr_direccion=$dir, usr_email=$ema, usr_telefono=$tel, usr_ci=$ci, per_cod=$per_co";
        $cns .= " where (usr_cod = $cod)";
	return $rs = $db->Execute($cns);
    }
    public function Eliminar($cod)
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
	$cns = "delete usuario where usr_cod=$cod";
	return $rs = $db->Execute($cns);
    }
    public function Obtener($cod)
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
	$cns = "select * from usuario where usr_cod = $cod";
	$rs = $db->Execute($cns);
        if($rs)
        {
            $dato = $rs->fetchRow();
            $this->usuario->setusr_cod($dato['usr_cod']);
            $this->usuario->setusr_username($dato['usr_username']);
            $this->usuario->setusr_password($dato['usr_password']);
            $this->usuario->setusr_email($dato['usr_email']);
            $this->usuario->setusr_direccion($dato['usr_direccion']);
            $this->usuario->setusr_nombre($dato['usr_nombre']);
            $this->usuario->setusr_apellidos($dato['usr_apellidos']);
            $this->usuario->setusr_ci($dato['usr_ci']);
            $this->usuario->setper_cod($dato['per_cod']);
            $this->usuario->setusr_telefono($dato['per_telefono']);
            return $this->usuario;
        }
        return null;
    }
    public function Seleccionar()
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
	$cns = "select * from usuario";
	$rs = $db->Execute($cns);
        $l = $rs->GetRows();
	return $l;
    }
}
?>
